package com.youmingxing.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import com.loopj.android.http.HttpGet;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by lang on 16/1/20.
 */
public class BitmapUtil {

    public  static Bitmap returnBitMap(String url) {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    public static Bitmap BitmapUtil(String urlstring) {
//        Bitmap bitmap =null;
//        try {
//            URL imageURl = null;
//            try {
//                imageURl = new URL(urlstring);
//                URLConnection con = imageURl.openConnection();
//                try {
//                    con.connect();
//                    InputStream in = con.getInputStream();
//                    bitmap  = BitmapFactory.decodeStream(in);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            } catch (MalformedURLException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        } finally {
//
//        }
        Bitmap bitMap = null;
        try {
            byte[] data = getImage(urlstring);
// File file = new File("1.jpg");
//OutputStream out = new FileOutputStream(file);
//out.write(data);
//out.close();
            int length = data.length;
            bitMap = BitmapFactory.decodeByteArray(data, 0, length);
        } catch (Exception e) {
        }
        LogUtil.i("-------bitmap--",urlstring +"  hight  " + bitMap.getHeight());
        return bitMap;
    }

    public static byte[] getImage(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
        httpURLconnection.setRequestMethod("GET");
        httpURLconnection.setReadTimeout(6 * 1000);
        InputStream in = null;
        if (httpURLconnection.getResponseCode() == 200) {
            in = httpURLconnection.getInputStream();
            byte[] result = readStream(in);
            in.close();
            return result;
        }
        return null;
    }

    public static byte[] readStream(InputStream in) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        in.close();
        return outputStream.toByteArray();
    }





    public interface Callback<T> {
        public void onSuccess(T obj);
        public void onError(String error);
    }



    public class GetBitmapResControl {

        private Activity activity;

        public GetBitmapResControl(Activity activity) {
            this.activity = activity;
        }

        public void doGetBitmap(final String url, final Callback<ImageView> callBack) {
            new Thread() {
                public void run() {
                    byte b[] = null;
                    try {
                        String _data = getImageData(url);//我这里的测试图片传入的是base64内容格式的.
                        if (_data != null) {
//                          、  b = Base64Util.decode(_data);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    final Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
                    activity.runOnUiThread(new Runnable() {
                        public void run() {
                            if (bitmap == null) {
                                callBack.onError("获取图片失败");
                            } else {
                                ImageView img = new ImageView(activity);
                                img.setImageBitmap(bitmap);
                                callBack.onSuccess(img);

                            }
                        }
                    });
                };
            }.start();
        }
        //获取网络中的图片内容
        private String getImageData(String url) throws ClientProtocolException, IOException {
            Log.d("getImageData", "URL:" + url);
            org.apache.http.client.HttpClient client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            HttpResponse httpResponse = client.execute(httpget);
            int status = httpResponse.getStatusLine().getStatusCode();
            if (status == HttpStatus.SC_OK) {
                Log.d("getImageData", "status:" + status);
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                return strResult;
            }
            return null;
        }

    }



}